package liumengbaio.lolzhangyoubao.Activity.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;

import liumengbaio.lolzhangyoubao.Activity.Activity.VideoThird_bodyActivity;
import liumengbaio.lolzhangyoubao.Activity.InFo.VideoSecondNewest;
import liumengbaio.lolzhangyoubao.R;

/**
 * 视频二级页面 适配器
 * <p/>
 * Created by lanouhn on 16/8/25.
 */
public class VideoSecondAdapter extends RecyclerView.Adapter {
    private VideoSecondNewest vsn;
    private Context mContext;
    
    public VideoSecondAdapter(VideoSecondNewest vsn, Context mContext) {
        this.vsn = vsn;
        this.mContext = mContext;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        RecyclerView.ViewHolder holder = null;
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_activity, null);
        holder = new BodyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        BodyViewHolder bodyViewHolder = (BodyViewHolder) holder;
        bodyViewHolder.tv_title.setText(vsn.getData().get(position).getTitle());
        Long datas = vsn.getData().get(position).getPublished() * 1000L;
        Date date = new Date(datas);
        SimpleDateFormat mFormat = new SimpleDateFormat("MM-dd HH:mm");
        final String time = mFormat.format(date);

//        Long published = vsn.getData().get(position).getPublished() * 1000L;
//        String dateStr = getDateFormatStr(published);

        bodyViewHolder.tv_time.setText(time);
        Picasso.with(mContext).load(vsn.getData().get(position).getPic_url()).into(bodyViewHolder.iv);

        bodyViewHolder.video_activity_rlbody.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String itemId = vsn.getData().get(position).getItem_id();
                String url = "http://api.lol.zhangyoubao.com/mobiles/item/" + itemId + "?v_=400901&size=middle&t=1472459388";
                Intent intent1 = new Intent(mContext, VideoThird_bodyActivity.class);
                intent1.putExtra("third_url", url);
                intent1.putExtra("video_url", vsn.getData().get(0).getVideo_url());
                mContext.startActivity(intent1);
            }
        });
    }

    @Override
    public int getItemCount() {
        return vsn.getData().size();
    }

    public class BodyViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv;
        private TextView tv_title, tv_time;
        private RelativeLayout video_activity_rlbody;

        public BodyViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.video_actItem_iv);
            tv_title = (TextView) itemView.findViewById(R.id.video_actItem_title);
            tv_time = (TextView) itemView.findViewById(R.id.video_actItem_time);
            video_activity_rlbody = (RelativeLayout) itemView.findViewById(R.id.video_activity_rlbody);
        }
    }


//    private String getDateFormatStr(long dateTimeStamp) {
//
//        String result;
//        long minute = 1000 * 60;
//        long hour = minute * 60;
//        long day = hour * 24;
//        long halfamonth = day * 15;
//        long month = day * 30;
//        long now = new Date().getTime();
//        long diffValue = now - dateTimeStamp;
//        if (diffValue < 0) {
//            //toast("结束日期不能小于开始日期！");
//        }
//        long monthC = diffValue / month;
//        long weekC = diffValue / (7 * day);
//        long dayC = diffValue / day;
//        long hourC = diffValue / hour;
//        long minC = diffValue / minute;
//
//        if (monthC >= 1) {
//            result = Integer.parseInt(monthC + "") + "个月前";
//            return result;
//        } else if (weekC >= 1) {
//            result = Integer.parseInt(weekC + "") + "周前";
//            return result;
//        } else if (dayC >= 1) {
//            result = Integer.parseInt(dayC + "") + "天前";
//            return result;
//        } else if (hourC >= 1) {
//            result = Integer.parseInt(hourC + "") + "个小时前";
//            return result;
//        } else if (minC >= 1) {
//            result = Integer.parseInt(minC + "") + "分钟前";
//            return result;
//        } else {
//            result = "刚刚发表";
//            return result;
//        }
//
//
//    }

}
